package org.moonlight.jvm.classfile.constant.info;

import org.moonlight.jvm.classfile.ClassReader;
import org.moonlight.jvm.classfile.constant.ConstantInfo;
import org.moonlight.jvm.classfile.constant.StaticConstantPool;

/**
 * CONSTANT_String_info常量表示java.lang.String字面量，结构如下：
 *   CONSTANT_String_info {
 *       u1  tag
 *       u2  string_index
 *   }
 *
 *   {@link ConstantInfo#CONSTANT_STRING}
 *
 * CONSTANT_String_info本身并不存放字符串数据，只存了常量池索引，这个索引指向一个CONSTANT_Utf8_info常量
 *
 * @author Moonlight
 * @createTime 2023/8/15 11:47
 **/
public class ConstantStringInfo implements ConstantInfo {

    private final StaticConstantPool scp;
    private int strIdx;

    public ConstantStringInfo(StaticConstantPool scp) {
        this.scp = scp;
    }

    @Override
    public void readInfo(ClassReader reader) {
        this.strIdx = reader.readUint16();
    }

    @Override
    public int tag() {
        return ConstantInfo.CONSTANT_STRING;
    }

    public String str() {
        return this.scp.getUtf8(this.strIdx);
    }

}
